const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    array: ['一次', '每天一次', '每天三次'],
    title: '',
    islogin: false,
    isadmin: false,
    voteuser: {},
    subtitle: '',
    type: 1,
    vsid: 0,
    statrdate: '请选择开始时间',
    enddate: '请选择结束时间',
    options: [{ voOption: '' }, { voOption: '' }]
  },
  bindPickerChange: function (e: any) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    });
  },
  checktype(e: any) {
    this.setData({ type: e.detail.value });
  },
  subtitleinput(e: any) {
    this.setData({ subtitle: e.detail.value })
  },
  addoption() {
    this.data.options.push({ voOption: '' });
    this.setData({ options: this.data.options });
  },
  deloption(e: any) {
    const index = e.currentTarget.dataset.id;
    this.data.options.splice(index, 1); // 根据索引删除对应的 input 框数据
    this.setData({ options: this.data.options });
  },
  getInputValue(e: any) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail.value;
    this.data.options[index].voOption = value; // 根据索引修改对应 input 的值
    this.setData({ options: this.data.options });
  },
  bindDateChange: function (e: any) {
    if ('end' == e.currentTarget.dataset.type) this.setData({ enddate: e.detail.value });
    else this.setData({ statrdate: e.detail.value });
  },

  optionrepeat(e: any) {
    let val = e.detail.value;
    let options = this.data.options;
    options.forEach((item, index) => {
      if (index !== e.currentTarget.dataset.index && item.voOption != '' && val === item.voOption) {
        wx.showToast({ title: '选项重复', icon: 'none' });
        item.voOption = ''
      }
    });
    this.setData({ options });
  },
  submitForm() {
    let ithis = this;
    if (!ithis.data.subtitle) {
      wx.showToast({ title: '请填写投票内容', icon: 'none' });
      return;
    }
    let options: any = [];
    ithis.data.options.forEach(item => {
      options.push(item.voOption)
    });
    options = options.filter((item: string) => item !== '')
    if (options.length < 2) {
      wx.showToast({ title: '选项内容需大于 2 项', icon: 'none' });
      return;
    }
    console.log(options);

    wx.request({
      url: 'http://localhost:9090/vote-subject/addSubject',
      method: 'GET',
      data: { 'title': ithis.data.subtitle, 'type': ithis.data.type, 'options': options, 'vsid': ithis.data.vsid, 'statrdate': this.data.statrdate, 'enddate': this.data.enddate, 'subjecttype': this.data.index + 1 },
      success(res: any) {
        if (res.data) {
          wx.showModal({
            title: '',
            content: '操作成功！',
            showCancel: false,
            confirmText: '确定',
            success: function (res) {
              if (res.confirm) ithis.find();
            }
          });
        }
      }
    });
  },
  change(e: any) {
    app.globalData.title = e.detail.value;
  },
  find() {
    wx.switchTab({ url: '../index/index' });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e: any) {
    let ithis = this;
    this.setData({
      voteuser: app.globalData.voteuser,
      islogin: app.globalData.islogin,
      title: app.globalData.title,
      isadmin: app.globalData.isadmin
    });
    if (e.vsid) {
      const vsid = e.vsid;
      wx.request({
        url: 'http://localhost:9090/vote-subject/findVSAndVoById',
        method: 'GET',
        data: { vsId: vsid },
        success(res: any) {
          ithis.setData({
            subtitle: res.data.vsTitle,
            options: res.data.options,
            type: res.data.vsType,
            vsid
          });
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})